<script type="text/javascript">
//文件树节点单击事件
function S_NodeClick(event, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj(treeId)

	if(treeNode.isParent) {
		zTree.expandNode(treeNode);
	} else {

		var $from = $("#"+ treeId).data("fromObj");
		var file_path = treeNode.url;
		
		if ($from && $from.length) $from.val(file_path);

		$("#j_select_filename_select_box").hide();

		if(!file_path) return;

		//先清空以前的资源列表
		$("#admin_resources_header").nextAll().remove();
		$("#admin_resource_edit input[name='source_name']").val('');
		$("#admin_resource_edit input[name='id']").val('');

		//ajax
		$.post("<?=admin_url('resources/ajax_get_source');?>", {file_path: file_path}, function(response){

			if(response.status_code == 200){
				var results = response.results.resources;

				var new_html = '';

				//处理多条记录
				var total_counts = results.length;

				$.each(results, function(k, item){
					var mod = k % 2;

					if(mod == 0){
						new_html += '<tr>' +
							'<td>' +
							'<label class="control-label" style="width:180px;">' + item.func + '：</label>' +
							'<input class="form-control" type="text" name="resources[' + item.func + ']" value="' + item.name + '" data-rule="required" size="16">' +
							'</td>';

						if((k + 1) >= total_counts){
							new_html += '<td><label class="control-label" style="width:180px;">&nbsp;</label>&nbsp;</td></tr>';
						}
					}else{
						new_html += '<td>' +
							'<label class="control-label" style="width:180px;">' + item.func + '：</label>' +
							'<input class="form-control" type="text" name="resources[' + item.func + ']" value="' + item.name + '" data-rule="required" size="16">' +
							'</td>' +
							'</tr>';
					}

				});

				$("#admin_resource_edit input[name='id']").val(response.results.id);
				$("#admin_resource_edit input[name='source_name']").val(response.results.source_name);
				$("#admin_resources_header").after(new_html);
				
			}else{
				$(this).alertmsg('error', response.warnings);
			}

		}, "json");
	}
   
    event.preventDefault();
}
</script>

<div class="bjui-pageContent">
	<div><h4><?php echo Iif($resource->id, '编辑资源', '添加资源');?></h4></div>
    <form action="<?=admin_url('resources/save');?>" class="pageForm" data-toggle="validate" name="admin_resource_edit" id="admin_resource_edit">
        <input type="hidden" name="id" value="<?=$resource->id;?>">
        <table class="table table-condensed table-hover">
            <tbody>
                <tr>
                    <td colspan="2">
                        <label class="control-label x90">选择文件：</label>

						<input type="text" name="file_path" id="j_ztree_filename" value="<?=$resource->file_path;?>" data-toggle="selectztree" data-tree="#j_select_filename" size="40" readonly style="cursor:pointer;">

						<ul id="j_select_filename" class="ztree hide" data-toggle="ztree" data-expand-all="false" data-check-enable="false" data-on-click="S_NodeClick">

							<?php foreach($admin_path_arr AS $key_1 => $val_1):?>
								<?php $key_1 += 1;?>

								<?php if(is_array($val_1['path'])):?>
									<li data-id="<?=$key_1;?>" data-pid="0" treenode_switch><?=$val_1['filename'];?></li>

									<?php foreach($val_1['path'] AS $key_2 => $val_2):?>
										<?php $key_2 += 1;?>

										<?php if(is_array($val_2['path'])):?>
											<li data-id="<?=$key_1 . $key_2;?>" data-pid="<?=$key_1;?>"><?=$val_2['filename'];?></li>

											<?php foreach($val_2['path'] AS $key_3 => $val_3):?>
												<?php $key_3 += 1;?>

												<?php if(is_array($val_3['path'])):?>

												<?php else:?>
													<li data-id="<?=$key_1 . $key_2 . $key_3;?>" data-pid="<?=$key_1 . $key_2;?>" data-faicon="caret-right" data-url="<?=$val_1['filename'];?>/<?=$val_2['filename'];?>/<?=$val_3['filename'];?>"><?=$val_3['filename'];?></li>
												<?php endif;?>

											<?php endforeach;?>

										<?php else:?>
											<li data-id="<?=$key_1 . $key_2;?>" data-pid="<?=$key_1;?>" data-faicon="caret-right" data-url="<?=$val_1['filename'];?>/<?=$val_2['filename'];?>"><?=$val_2['filename'];?></li>
										<?php endif;?>

									<?php endforeach;?>

								<?php else:?>
									<li data-id="<?=$key_1;?>" data-pid="0" data-faicon="caret-right" data-url="<?=$val_1['filename'];?>"><?=$val_1['filename'];?></li>
								<?php endif;?>

							<?php endforeach;?>
						</ul>

                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <label class="control-label x90">资源名称：</label>
						<input class="form-control" type="text" name="source_name" value="<?=$resource->source_name;?>" data-rule="required" size="26">
                    </td>
                </tr>
                <tr id="admin_resources_header">
                    <td>
                        <label class="control-label" style="font-weight:normal;color:#959595;width:180px;">权限代码：</label>
                        <label class="control-label" style="font-weight:normal;color:#959595;width:160px;text-align:center;">名称</label>                        
                    </td>
                    <td>
                        <label class="control-label" style="font-weight:normal;color:#959595;width:180px;">权限代码：</label>
                        <label class="control-label" style="font-weight:normal;color:#959595;width:160px;text-align:center;">名称</label>                        
                    </td>
                </tr>
				<?php if(!empty($resources_list)):?>
				<?php foreach($resources_list AS $key => $val):?>

					<?php $mod = $key % 2;?>

					<?php if($mod == 0):?>
						<tr><td><label class="control-label" style="width:180px;"><?=$val['func'];?>：</label><input class="form-control" type="text" name="resources[<?=$val['func'];?>]" value="<?=$val['name'];?>" data-rule="required" size="16"></td>

						<?php if(($key + 1) >= $resources_count):?>
							<td><label class="control-label" style="width:180px;">&nbsp;</label>&nbsp;</td></tr>
						<?php endif;?>
					<?php else:?>
						<td><label class="control-label" style="width:180px;"><?=$val['func'];?>：</label><input class="form-control" type="text" name="resources[<?=$val['func'];?>]" value="<?=$val['name'];?>" data-rule="required" size="16"></td></tr>
					<?php endif;?>

				<?php endforeach;?>
				<?php endif;?>
            </tbody>
        </table>
    </form>
</div>
<div class="bjui-pageFooter">
    <ul>
        <li><button type="button" class="btn-close">关闭</button></li>
        <li><button type="submit" class="btn-green">保存</button></li>
    </ul>
</div>